﻿using Nancy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JWT;
using CustomCommon;

namespace jczUserService.Modules
{
    public class IndexModule : NancyModule
    {
        public IndexModule()
        {
            /*Get["/index"] = x => 
            {
                return View["index"];
            };*/
            Before += ctx => {
                /*string sjwt = this.Context.Request.Session["UserToken"] == null ? "": this.Context.Request.Session["UserToken"].ToString();
                string SecretKey = "9fdb64ec22e9cda94ad9964d07dd9dd16ee8c41a8e0d4367e1d09d2520ae583cb8357a992edb76a74a4b866243580b6106f6497733641f588f8608f391443138";
                byte[] byteArray = System.Text.Encoding.Default.GetBytes(SecretKey);
                bool tokenVerification = JsonWebToken.VerificationToken(sjwt, byteArray);
                if(!tokenVerification)
                    return Response.AsRedirect("/login");
                System.Diagnostics.Debug.WriteLine("indexModule---Before-------------" + sjwt);*/
                return null;
            };
            Get["/index"] = x =>
            {
                string s_uid = Request.Query["uid"];//get请求获取方法
                string sjwt = "";
                object obj = MemcacheHelper.Get(s_uid);//根据key从Memcache中获取用户的信息
                if (obj != null)
                {
                    sjwt = obj.ToString(); 
                }
                
                //if (this.Context.Request.Session["UserToken"] != null)
                //    sjwt = this.Context.Request.Session["UserToken"].ToString();//
                //this.Context.Request.Cookies.Add("UserToken", sjwt); //
                                                                     //this.Context.Response.WithCookie("UserToken", sjwt);
                return View["index", sjwt];
            };// getindex;
        }

    }
}
